
$identify("org/mathdox/formulaeditor/modules/relation2/eqs.js");

$require("org/mathdox/parsing/ParserGenerator.js");
$require("org/mathdox/formulaeditor/parsing/openmath/OpenMathParser.js");
$require("org/mathdox/formulaeditor/parsing/expression/ExpressionParser.js");
$require("org/mathdox/formulaeditor/semantics/MultaryOperation.js");

$main(function(){

  /**
   * Define a semantic tree node that represents relation2.eqs.
   */
  org.mathdox.formulaeditor.semantics.Relation2Eqs =
    $extend(org.mathdox.formulaeditor.semantics.MultaryOperation, {

      symbol : {

        onscreen : "=",
        openmath : "<OMS cd='relation2' name='eqs'/>",
        mathml   : "<mo>=</mo>"

      },

      precedence : 110

    });
  
  /**
   * Extend the OpenMathParser object with parsing code for relation2.eqs.
   */
  org.mathdox.formulaeditor.parsing.openmath.OpenMathParser =
    $extend(org.mathdox.formulaeditor.parsing.openmath.OpenMathParser, {

    /**
     * Returns an equality object based on the OpenMath node.
     */
    handleRelation2Eqs : function(node) {

      // parse the children of the OMA
      var children = node.getChildNodes();
      var operands = [];
      for (var i=1; i<children.length; i++) {
        operands.push(this.handle(children.item(i)));
      }

    }

  });

  /**
   * Extend the ExpressionParser object with parsing code for the equality sign.
   */
  var semantics = org.mathdox.formulaeditor.semantics;
  var pG = new org.mathdox.parsing.ParserGenerator();

  if ( "=" == "=" ) {
    // only one expression, same on screen
    org.mathdox.formulaeditor.parsing.expression.ExpressionParser =
      $extend(org.mathdox.formulaeditor.parsing.expression.ExpressionParser, {

    // expression110 = eqs | super.expression110
      expression110 : function() {
        var parent = arguments.callee.parent;
        pG.alternation(
          pG.rule("relation2eqs"),
          parent.expression110).apply(this, arguments);
      },

      // relation2eqs = 
      //    expression110 "=" expression120
      relation2eqs :
        pG.transform(
          pG.concatenation(
            pG.rule("expression120"),
            pG.literal("="),
            pG.rule("expression120"),
            pG.repetitionplus(
              pG.concatenation(
                pG.literal("="),
                pG.rule("expression120")
              )
            )
          ),
          function(result) {
            var retval = new semantics.Relation2Eqs();
            var operands = [];
            var i;

            for (i=0; 2*i<result.length; i++) {
              operands[i] = result[2*i];
            }
            retval.operands = operands;

            return retval;
          }
        )

    });
  } else { // allow alternative as displayed on the screen
    org.mathdox.formulaeditor.parsing.expression.ExpressionParser =
      $extend(org.mathdox.formulaeditor.parsing.expression.ExpressionParser, {

      // expression110 = relation2eqs | 
      //   relation2eqsalt | super.expression110
      expression110 : function() {
        var parent = arguments.callee.parent;
        pG.alternation(
          pG.rule("relation2eqs"),
          pG.rule("relation2eqsalt"),
          parent.expression110).apply(this, arguments);
      },

      // relation2eqs = 
      //    expression110 "=" expression120
      relation2eqs :
        pG.transform(
          pG.concatenation(
            pG.rule("expression110"),
            pG.literal("="),
            pG.rule("expression120"),
            pG.repetitionplus(
              pG.concatenation(
                pG.literal("="),
                pG.rule("expression120")
              )
            )
          ),
          function(result) {
            var retval = new semantics.Relation2Eqs();
            var operands = [];
            var i;

            for (i=0; 2*i<result.length; i++) {
              operands[i] = result[2*i];
            }
            retval.operands = operands;

            return retval;
          }
        ),

      // relation2eqsalt = 
      //    expression110 "=" expression120
      relation2eqsalt :
        pG.transform(
          pG.concatenation(
            pG.rule("expression110"),
            pG.literal("="),
            pG.rule("expression120"),
            pG.repetitionplus(
              pG.concatenation(
                pG.literal("="),
                pG.rule("expression120")
              )
            )
          ),
          function(result) {
            var retval = new semantics.Relation2Eqs();
            var operands = [];
            var i;

            for (i=0; 2*i<result.length; i++) {
              operands[i] = result[2*i];
            }
            retval.operands = operands;

            return retval;
          }
        )

    });
  }
});
